package com.gyg.interceptor;

import com.gyg.domain.CloudUser;
import com.gyg.enums.SessionEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

public class ForceLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();

        CloudUser cloudUser = (CloudUser) session.getAttribute(SessionEnum.USER.getCode());

        if(Objects.isNull(cloudUser)){
            response.sendRedirect("auth-sign-in.html");
            return false;
        }

        String block = (String) session.getAttribute(SessionEnum.BLOCK.getCode());
        //锁屏状态
        if (StringUtils.isNotBlank(block)) {
            response.sendRedirect("auth-lock-screen.html");
            return false;
        }

        return true;
    }
}
